﻿using System;
using Cat;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmSubCategory : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcConnection con = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbconn = new DatabaseConnection();
        public frmSubCategory()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmSubCategory_Load);
            btnNew.Click += new EventHandler(btnNew_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnGetData.Click += new EventHandler(btnGetData_Click);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            cmbCategory.SelectedIndexChanged += new EventHandler(cmbCategory_SelectedIndexChanged);
            cmbCategory.Click += new EventHandler(cmbCategory_Click);
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtSubCategoryName.Text == "")
            {
                MessageBox.Show("Please Select Sub-Category Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCategoryName.Focus();
                return;
            }
            if (cmbCategory.Text == "")
            {
                MessageBox.Show("Please Select Category Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCategoryName.Focus();
                return;
            }
            dbconn.dbconnection();
            dbconn.UpdateData("Update tblsubcategory set SubCname='" + txtSubCategoryName.Text + "',CategoryName='"+cmbCategory.Text+"' Where SubID='" + textBox1.Text + "';");
            MessageBox.Show("Update Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            reset();
            dbconn.DBClose();  
        }

        void btnGetData_Click(object sender, EventArgs e)
        {
            frmSubcategorylist frmS = new frmSubcategorylist();
            frmS.Show();
            this.Close();
        }
        void cmbCategory_Click(object sender, EventArgs e)
        {
            txtSubCategoryName.Enabled = true;        
        }
        void frmSubCategory_Load(object sender, EventArgs e)
        {
            fillcombo();
            txtSubCategoryName.Focus();
            
        }
        void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            cmd = con.CreateCommand();

            cmd.CommandText = "SELECT * from tblcategory WHERE CategoryName = '" + cmbCategory.Text + "'";
            rdr = cmd.ExecuteReader();

            if (rdr.Read())
            {
                txtCategoryName.Text = cmbCategory.Text;
                con.Close();
            }
            if ((rdr != null))
            {
                rdr.Close();
            }
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            if (txtSubCategoryName.Text == "")
            {
                MessageBox.Show("Please enter Sub-Category name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSubCategoryName.Focus();
                return;
            }

            try
            {

                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select SubCName from tblsubcategory where SubCName = '" + txtSubCategoryName.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Sub-Category Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtSubCategoryName.Text = "";
                    txtSubCategoryName.Focus();

                    if ((rdr != null))
                    {
                        rdr.Close();
                    }
                    return;
                }

                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string cb = "Insert into tblsubcategory(SubCname,CategoryName) VALUES ('" + txtSubCategoryName.Text + "','" + txtCategoryName.Text + "')";
                cmd = new OdbcCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);

                btnSave.Enabled = false;
                txtSubCategoryName.Enabled = false;
                reset();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void btnNew_Click(object sender, EventArgs e)
        {
            reset();
        }
        void reset()
        {
            txtSubCategoryName.Text = "";
            cmbCategory.Text = null;
            btnUpdate.Enabled = false;
            txtSubCategoryName.Enabled = false;
            btnSave.Enabled = true;
        
        }
        void fillcombo()
        {
            try
            {
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "select RTRIM(CategoryName) from tblcategory order by CategoryName";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    cmbCategory.Items.Add(rdr[0]);
                }
                con.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }    
        }
        

    }

}
